const xlsx = require('node-xlsx');
const fs =  require('fs');
const request = require('request');
const sleep = require('sleep');
const data = [];
const title = ['id', '店铺名name', '开始时间start', '结束时间end', '场景scene', '类型type', '成功success', '错误error', 'submit_items_id', 'error_items_id', 'request_id', 'activity_name活动名', 'activity_id'];
data.push(title);
for (let i = 1; i < 12; i++) {
    start(i);
}


function start(i) {
    request({
        url: `http://zsadmin.huanleguang.com/registration/plugin/log/user_info?page=${i}&page_size=20000 `, // 你要请求的地址
        method: 'get', // 请求方法 post get
        json: true,
        headers: {
            'content-type': 'application/json',
            Cookie: 'user_device_id=d19136942de24c9698cd956399167ef1;\
             user_device_id_timestamp=1601284085263; _ga=GA1.2.1556531631.1601288172;\
              _ati=94498012293; sensorsdata2015jssdkcross=%7B%22distinct_id%22\
              %3A%223215139%22%2C%22%24device_id%22%3A%221751c3d9e3cdf4-0b0bdfb91fd31a-3\
              1697305-3686400-1751c3d9e3de94%22%2C%22props%22%3A%7B%22%24latest_referrer%\
              22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_s\
              ource_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_searc\
              h_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93\
              %E5%BC%80%22%7D%2C%22first_id%22%3A%221751c3d9e3cdf4-0b0bdfb91fd31a-31697305-3686400\
              -1751c3d9e3de94%22%7D; PHPSESSID=hlqf0bmt9sdslv6t0gotv7cvto',
        },
    }, function (error, response, body) {
        console.log(error, 'error');
        if (!error && response.statusCode == 200) {
            const list = body.result.list;
            list.forEach(item => {
                const arr = Object.entries(item).map(([key, value]) => {
                    if (['start', 'end'].includes(key)) {
                        return timeFomate(value);
                    }
                    return value;
                });
                data.push(arr);
            });
        }
    });
}

// 写xlsx文件
function writeXls(datas) {
    const buffer = xlsx.build([
        {
            name: 'sheet1',
            data: datas,
        },
    ]);
    fs.writeFileSync('./data.xlsx', buffer, { flag: 'w+' });// 生成excel data是excel的名字
}

function timeFomate(str) {
    const date = new Date(Number(str));
    const Y = date.getFullYear() + '-';
    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    const D = date.getDate() + ' ';
    return Y + M + D;
}

setTimeout(() => {
    const dataList = [
        '2020-10-15', '2020-10-16', '2020-10-17', '2020-10-18', '2020-10-19', '2020-10-20', '2020-10-21', '2020-10-22', '2020-10-23', '2020-10-24',
        '2020-10-25', '2020-10-26', '2020-10-27', '2020-10-28', '2020-10-29', '2020-10-30', '2020-10-31', '2020-11-1', '2020-11-2',
        '2020-11-3', '2020-11-4', '2020-11-5', '2020-11-6', '2020-11-7', '2020-11-8', '2020-11-9', '2020-11-10', '2020-11-11', '2020-11-12',
        '2020-11-13', '2020-11-14', '2020-11-15',
    ];

    const list = data.filter(item => {
        let status = false;
        item.forEach(value => {
            if (dataList.includes(typeof value === 'string' ? value.trim() : value)) {
                status = true;
            }
        });
        return status;
    });
    // writeXls(list);
    const result = {};
    dataList.forEach(key => {
        result[key] = list.filter(item => {
            let status = false;
            item.forEach(value => {
                if ((typeof value === 'string' ? value.trim() : value) === key) {
                    status = true;
                }
            });

            return status && item.includes('tmc');
        });
    });

    // 去重
    const res = {};
    Object.entries(result).forEach(([key, arr]) => {
        const newArr = [];
        arr.forEach(item => {
            // let status = false;
            // newArr.forEach(i => {
            //     if (Number(i[1]) === Number(item[1])) {
            //         status = true;
            //     }
            // });
            // if (!status) {
            //     newArr.push(item);
            // }
            newArr.push(item[1]);
        });
        res[key] = Array.from(new Set(newArr)).length;
    });

    console.log(res);
}, 40000);
